package com.longjianghuasheng.logistics.business.order.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.lifecycle.viewModelScope
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseTitleFragment
import com.longjianghuasheng.logistics.business.order.adapter.DriverOrderAdapter.Companion.CANCEL_APPLICATION_FINISHED
import com.longjianghuasheng.logistics.business.order.adapter.DriverOrderAdapter.Companion.CANCEL_APPLY_FOR
import com.longjianghuasheng.logistics.business.order.adapter.DriverOrderAdapter.Companion.CANCEL_DELETE_THE_ORDER
import com.longjianghuasheng.logistics.business.order.viewmodel.DriverOrderDetailViewModel
import com.longjianghuasheng.logistics.business.order.viewmodel.DriverOrderViewModel
import com.longjianghuasheng.logistics.databinding.FragmentDriverOrderDetailBinding
import com.longjianghuasheng.logistics.entity.Order
import com.longjianghuasheng.logistics.eventViewModel
import com.longjianghuasheng.logistics.ext.deleteDialog
import com.longjianghuasheng.logistics.ext.observer
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import me.hgj.jetpackmvvm.ext.view.visible

//司机订单详情
class DriverOrderDetailFragment :
    BaseTitleFragment<DriverOrderDetailViewModel, FragmentDriverOrderDetailBinding>() {
    private val driverOrderViewModel by viewModels<DriverOrderViewModel>()
    override fun layoutId(): Int = R.layout.fragment_driver_order_detail
    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        initTitleBar("我的订单")
        mDatabind.run {

            btnCommit.setOnClickListener {
                when (btnCommit.text.toString()) {
                    CANCEL_APPLY_FOR -> driverOrderViewModel.driver_order_cancel()
                    CANCEL_APPLICATION_FINISHED -> driverOrderViewModel.driver_order_done()
                    CANCEL_DELETE_THE_ORDER -> deleteDialog {

                        driverOrderViewModel.driver_order_del()
                    }
                    else -> {}
                }

            }

        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.driverResult.observer(this) {

            mDatabind.setData(it)
        }
        driverOrderViewModel.notify.observer(this) {
            navigateUp()
        }
        driverOrderViewModel.deleteNotify.observer(this) {

            driverOrderViewModel.viewModelScope.launch {
                eventViewModel.onOrderChangeEvent.value = 0
                delay(300L)
                navigateUp()
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun FragmentDriverOrderDetailBinding.setData(it: Order) {

        it.run {

            stvStart.setCenterString(start_address)
            stvEnd.setCenterString(end_address)
            stvName.setCenterString(names)
            stvNum.setCenterString(nums)
            stvWeight.setCenterString(weights)
            stvVolume.setCenterString(volume)

            tvOrderNum.text = "订单编号：$trade_no"
            tvDate.text = "下单日期：$create_time"
            tvPrice.text = "￥$price"
            btnCommit.text = when (mViewModel.status) {
                1 -> CANCEL_APPLY_FOR
                2 -> CANCEL_APPLICATION_FINISHED
                3 -> CANCEL_DELETE_THE_ORDER
                else -> ""
            }
        }

        clBg.visible()
    }

    override fun initData() {
        super.initData()
        arguments?.run {
            getInt("id").let {
                mViewModel.id = it
                driverOrderViewModel.id = it
            }
            mViewModel.status = getInt("status")
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        mViewModel.driver_order_show()
    }

}